package com.example.liunanxin.lnx226ordering.model;


import com.example.liunanxin.lnx226ordering.beans.FoodCommentsBean;
import com.example.liunanxin.lnx226ordering.iface.FoodCommentsListener;
import com.example.liunanxin.lnx226ordering.iface.FoodCommentsiface;
import com.example.liunanxin.lnx226ordering.retrofitservice.FoodCommentsService;
import com.example.liunanxin.lnx226ordering.url.Url;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodCommentsModel implements FoodCommentsiface {
    private Retrofit retrofit;

    public FoodCommentsModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Url.USERURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getFoodCommentsResult(int food_id, final FoodCommentsListener foodCommentsListener) {
        FoodCommentsService foodCommentsService=retrofit.create(FoodCommentsService.class);
        Call<List<FoodCommentsBean>> call=foodCommentsService.getFoodComments(food_id);
        call.enqueue(new Callback<List<FoodCommentsBean>>() {
            @Override
            public void onResponse(Call<List<FoodCommentsBean>> call, Response<List<FoodCommentsBean>> response) {

                if(response.isSuccessful()&& response!=null){
                    foodCommentsListener.onResponse(response.body());
                }
                else foodCommentsListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<FoodCommentsBean>> call, Throwable t) {

                foodCommentsListener.onFail(t.toString());
            }
        });
    }
}
